package com.zaimokuza.cn.controller;

import com.zaimokuza.cn.entity.PageResult;
import com.zaimokuza.cn.entity.auth.Resources;
import com.zaimokuza.cn.entity.auth.RoleResourcesRelation;
import com.zaimokuza.cn.entity.response.ResponseFormat;
import com.zaimokuza.cn.service.ResourcesService;
import com.zaimokuza.cn.util.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @author zaimokuza
 */
@Api(tags = "资源相关")
@ResponseFormat
@RestController
@RequestMapping("/resources")
public class ResourcesController {

    @Resource
    private ResourcesService resourcesService;

    @ApiOperation(value = "添加资源", notes = "添加资源")
    @PostMapping("/add")
    public void addResources(@Validated @RequestBody Resources resources) {
        // 设置创建者
        resources.setCreatedBy(UserUtils.getUserId());
        // 添加资源
        resourcesService.addResources(resources);
    }

    @ApiOperation(value = "更新资源", notes = "根据资源ID更新资源")
    @PutMapping("/update")
    public void updateResources(@Validated @RequestBody Resources resources) {
        // 设置更新者
        resources.setUpdatedBy(UserUtils.getUserId());
        // 更新资源
        resourcesService.updateResources(resources);
    }

    @ApiOperation(value = "更新角色资源关联信息", notes = "更新角色资源关联信息")
    @PutMapping("/relation/update")
    @Transactional(rollbackFor = Exception.class)
    public void updateRoleResourcesRelations(@Validated @RequestBody RoleResourcesRelation relations) {
        resourcesService.updateRoleResourcesRelations(relations);
    }

    @ApiOperation(value = "删除资源", notes = "根据资源ID删除资源")
    @DeleteMapping("/delete")
    @Transactional(rollbackFor = Exception.class)
    public void deleteResources(@RequestParam @ApiParam("资源ID") Long id) {
        resourcesService.deleteResources(id);
    }

    @ApiOperation(value = "查询资源树", notes = "查询资源树")
    @GetMapping("/tree")
    ArrayList<Resources> getResourcesTree(@RequestParam(required = false) @ApiParam("忽略的节点资源ID") Long ignoreId) {
        return resourcesService.getResourcesTree(ignoreId);
    }

    @ApiOperation(value = "查询角色ID拥有的资源信息", notes = "查询角色ID拥有的资源信息")
    @GetMapping("/role")
    ArrayList<Long> getRoleResources(@RequestParam @ApiParam("角色ID") String roleId) {
        return resourcesService.getResourcesIdListByRoleId(roleId);
    }

    @ApiOperation(value = "查询资源分页信息", notes = "查询资源分页信息")
    @GetMapping("/page")
    PageResult<Resources> getResourcesPage(@RequestParam(required = false) @ApiParam("资源名称") String name,
                                           @RequestParam(required = false) @ApiParam("资源请求路径") String url,
                                           @RequestParam(required = false) @ApiParam("资源类型") Integer type,
                                           @RequestParam(defaultValue = "1") @ApiParam("当前页") Integer page,
                                           @RequestParam(defaultValue = "10") @ApiParam("每页条数") Integer size) {
        return resourcesService.getResourcesPage(name, url, type, page, size);
    }

    @ApiOperation(value = "根据ID查询资源详情", notes = "根据ID查询资源详情")
    @GetMapping("/detail")
    Resources getResourcesPage(@RequestParam @ApiParam("资源ID") Long id) {
        return resourcesService.getResources(id);
    }

}
